#include "myBank.h"
#include "ReadFile.h"

bank::bank(QWidget* parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    this->clientCount = 0;
    this->curClient = 0;
    connect(ui.add, &QPushButton::clicked, this, &bank::addClient);
    connect(ui.trading, &QPushButton::clicked, this, &bank::trade);
    connect(ui.randomAdd, &QPushButton::clicked, this, &bank::random);
    connect(ui.update, &QPushButton::clicked, this, &bank::update);
    connect(ui.reset, &QPushButton::clicked, this, &bank::reset);
    connect(ui.closure, &QPushButton::clicked, this, &bank::close);
    connect(ui.sort, &QPushButton::clicked, this, &bank::sort);
    connect(ui.readFile, &QPushButton::clicked, this, &bank::readFile);
    connect(ui.readFile_2, &QPushButton::clicked, this, &bank::readFile_2);
    connect(ui.check_button, &QPushButton::clicked, this, &bank::check);
    connect(ui.select, &QComboBox::currentTextChanged, this, &bank::reshow);
    // 静止编辑
    ui.TbankWidgets->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers);
    ui.Tstatistics->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers);
    // 初始化 窗口列表
    for (int i = 0; i < 10; i++) {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setTextAlignment(Qt::AlignCenter);
        ui.TbankWidgets->setItem(0, i, item);
    }
    //设置窗口图标
    this->setWindowIcon(QIcon("银行-中国银行.png"));
    this->setWindowTitle("银行");
    //设置鼠标图标
    QPixmap pixmap = QPixmap("光标.png");
    pixmap = pixmap.scaled(20, 20, Qt::KeepAspectRatio);
    QCursor cursor = QCursor(pixmap);
    setCursor(cursor);
    // 初始化链表
    this->list = *new LinkList<Client>();
}

bank::~bank()
{}

// 弹窗提示
void bank::pop_up(QMessageBox::Icon style,QString title,QString content) {
    QMessageBox typeWarning(style, title, content);
    if (style == QMessageBox::Icon::Warning) {
        typeWarning.setWindowIcon(QIcon("警告.png"));
    }
    if (style == QMessageBox::Icon::Information) {
        typeWarning.setWindowIcon(QIcon("提示.png"));
    }
    typeWarning.exec();
}
// 使用快速排序根据到达时间 编号
void bank::numbering() {
    CallerMachine caller;
    // 快速排序
    quickSort<Client>::qSort(list.getNode(curClient), list.getNode(clientCount));
    // 重新排号
    for (int i = 0; i < clientCount; i++) {
        list.getElement(i)->setNo(caller.getCurrentNo());
    }
}
// 添加客户行 （在页面输入必须项后，需要更新）
void bank::addClient() {
    if (ui.TbankClient->rowCount() < this->clientCount + 1) {
        ui.TbankClient->insertRow(clientCount);
        for (int i = 0; i < 9; i++) {
            // 初始化item 并设置居中 防止NULL
            QTableWidgetItem * item = new QTableWidgetItem("");
            ui.TbankClient->setItem(clientCount, i, item);
            item->setTextAlignment(Qt::AlignCenter);
        }
    }
    // 更新客户总数
    this->list.insert(Client(), clientCount);
    clientCount++;
}
// 营业 入队一个客户
void bank::trade() {
    if (!update())   return;
    // 判断是否有等待的客户
    if (clientCount > curClient) {
        //获取窗口
        Widgets* widgets = &this->my_bank.getWidgets();
           // list.getElement(curClient)->  替换 clients[curClient]
        if (list.getElement(curClient)->getBussinessType() == BussinessType::COMMON) {

            // 客户进入窗口（入队）  
            list.getElement(curClient)->setWidgetNo(widgets[1].num);
            int subtract = widgets[1].widget.FinTime() - list.getElement(curClient)->getReachTime() > 0 ? widgets[1].widget.FinTime() - list.getElement(curClient)->getReachTime() : 0;
            list.getElement(curClient)->setWaitingTime(subtract);
            widgets[1].widget.getQueue().push(*list.getElement(curClient));
            //并更新窗口信息
            widgets[1].widget.FinTime() = list.getElement(curClient)->getOverTime();
            widgets[1].widget.getCurNum() = list.getElement(curClient)->getNo();
            widgets[1].widget.setNumInQu() += 1;
            this->my_bank.heapAdjust(WidgetType::COMMON);
        }
        else {
            int mini = 0x3f3f3f3f;
            int mininum = -1;
            //寻找结束时间最小的特殊窗口
            for (int i = 1; i <= 10; i++) {
                if (widgets[i].type == WidgetType::SPECIAL && mini > widgets[i].widget.FinTime()) {
                    mininum = i;
                    mini = widgets[i].widget.FinTime();
                }
            }

            //widgets[mininum].widget.getQueue().push(clients[curClient]);
            //客户进入窗口（入队）
            list.getElement(curClient)->setWidgetNo(widgets[mininum].num);
            int subtract = widgets[mininum].widget.FinTime() - list.getElement(curClient)->getReachTime() > 0 ? widgets[mininum].widget.FinTime() - list.getElement(curClient)->getReachTime() : 0;
            list.getElement(curClient)->setWaitingTime(subtract);
            widgets[mininum].widget.getQueue().push(*list.getElement(curClient));
            // 更新窗口信息
            widgets[mininum].widget.FinTime() = list.getElement(curClient)->getOverTime();
            widgets[mininum].widget.getCurNum() = list.getElement(curClient)->getNo();
            widgets[mininum].widget.setNumInQu() += 1;
            this->my_bank.heapAdjust(WidgetType::SPECIAL);
        }
        // 设置界面变化
        for (int j = 6; j < 9; j++) {
            QTableWidgetItem* item = ui.TbankClient->item(curClient,j);
            QString Text; quint16 data;
            switch (j) {
                case 6:
                    Text = QString::number(list.getElement(curClient)->getWaitingTime());
                    item->setText(Text);
                    break;
                case 7:
                    Text = QString::fromStdString(list.getElement(curClient)->getOverDate().getTime());
                    item->setText(Text);
                    break;
                case 8:
                    data = list.getElement(curClient)->getWidgetNo();
                    item->setData(Qt::DisplayRole,data);
                    break;
            }

        }
        //设置窗口当前客户
        QString Text = QString::fromStdString(list.getElement(curClient)->getNo());
        QTableWidgetItem *item = this->ui.TbankWidgets->item(0, list.getElement(curClient)->getWidgetNo() - 1);
        item->setText(Text);
        
        // 剔除当前时刻已经完成业务的客户
        Widgets* widget = &my_bank.getWidgets();
        int time =  this->list.getElement(curClient)->getReachTime();
        for (int i = 1; i < 11; i++) {
            Client client;
            if (widget[i].widget.getQueue().top(client)) {
                if (client.getOverTime() < time) {
                    widget[i].widget.getQueue().pop();
                    this->ui.TbankWidgets->item(0,client.getWidgetNo() - 1)->setText("");
                }
            }
        }
        if (list.getElement(curClient)->getReachTime() == 540) {
            close();
        }
        curClient++;
    }
}
// 生成随机客户
void bank::random() {
    //先随机生成客户
    CallerMachine caller;
    for (int i = 1; i <= curClient; i++)     caller.getCurrentNo();
    // 设置随机生成客户的到达最晚时间
    int min;
    if (curClient != 0) {
        min = list.getElement(curClient - 1)->getReachTime();
    }
    else {
        min = 0;
    }
    for (int i = curClient; i < clientCount; i++) {
        this->list.setElement(*new Client(caller, min), i);
    }
    //重新编号
    numbering();
    // 输出到页面
    for (int i = curClient; i < clientCount; i++) {
        for (int j = 0; j < 6; j++) {
            QString Text;
            switch (j) {
                case 0:
                    Text = QString::fromStdString(list.getElement(i)->getNo());    
                    break;
                case 1:
                    Text = QString::fromStdString(list.getElement(i)->getName());
                    break;
                case 2:
                    if(list.getElement(i)->getSex() == Sex::MAN)     Text = "男";
                    else  Text = "女";
                    break;
                case 3:
                    if (list.getElement(i)->getBussinessType() == BussinessType::COMMON)   Text = "一般业务";
                    else    Text = "特殊业务";
                    break;
                case 4:
                    Text = QString::fromStdString(list.getElement(i)->getReachDate().getTime());
                    break;
                case 5:
                    Text = QString::number(list.getElement(i)->getComsumptionTime());
                    break;
            }
            this->ui.TbankClient->item(i, j)->setText(Text);
        }
    }
}
// 从文件中读取客户
void bank::readFile() {
    // 获取文件路径
    QString url = ui.fileName->text();
    this->clientCount = read(url.toStdString(), list,this->curClient);
    // 重新编号
    numbering();
    // 确保表格有足够多的行
    int rowCount = ui.TbankClient->rowCount();
    while(rowCount < clientCount) {
        ui.TbankClient->insertRow(rowCount);
        for (int i = 0; i < 9; i++) {
            // 初始化item 并设置居中 防止NULL
            QTableWidgetItem* item = new QTableWidgetItem("");
            ui.TbankClient->setItem(rowCount, i, item);
            item->setTextAlignment(Qt::AlignCenter);
        }
        rowCount++;
    }
    // 输出到页面
    for (int i = curClient; i < clientCount; i++) {
        for (int j = 0; j < 6; j++) {
            QTableWidgetItem* item = ui.TbankClient->item(i,j);
            QString Text;
            switch (j) {
            case 0:Text = QString::fromStdString(list.getElement(i)->getNo()); break;
            case 1:Text = QString::fromStdString(list.getElement(i)->getName()); break;
            case 2:
                if (list.getElement(i)->getSex() == Sex::MAN)   Text = "男";
                else    Text = "女";
                break;
            case 3:
                if (list.getElement(i)->getBussinessType() == BussinessType::COMMON)   Text = "一般业务";
                else    Text = "特殊业务";
                break;
            case 4:Text = QString::fromStdString(list.getElement(i)->getReachDate().getTime()); break;
            case 5:Text = QString::number(list.getElement(i)->getComsumptionTime()); break;
            }
            item->setText(Text);
        }
    }
}
// 打开文件夹选择文件 并读取
void bank::readFile_2() {
    // 选择文件
    QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Text Files (*.txt)");
    this->clientCount = read(fileName.toStdString(), list,this->curClient);
    // 重新编号
    numbering();
    // 确保表格有足够多的行
    int rowCount = ui.TbankClient->rowCount();
    while (rowCount < clientCount) {
        ui.TbankClient->insertRow(rowCount);
        for (int i = 0; i < 9; i++) {
            // 初始化item 并设置居中 防止NULL
            QTableWidgetItem* item = new QTableWidgetItem("");
            ui.TbankClient->setItem(rowCount, i, item);
            item->setTextAlignment(Qt::AlignCenter);
        }
        rowCount++;
    }
    // 输出到页面
    for (int i = curClient; i < clientCount; i++) {
        for (int j = 0; j < 6; j++) {
            QTableWidgetItem* item = ui.TbankClient->item(i, j);
            QString Text;
            switch (j) {
            case 0:Text = QString::fromStdString(list.getElement(i)->getNo()); break;
            case 1:Text = QString::fromStdString(list.getElement(i)->getName()); break;
            case 2:
                if (list.getElement(i)->getSex() == Sex::MAN)   Text = "男";
                else    Text = "女";
                break;
            case 3:
                if (list.getElement(i)->getBussinessType() == BussinessType::COMMON)   Text = "一般业务";
                else    Text = "特殊业务";
                break;
            case 4:Text = QString::fromStdString(list.getElement(i)->getReachDate().getTime()); break;
            case 5:Text = QString::number(list.getElement(i)->getComsumptionTime()); break;
            }
            item->setText(Text);
        }
    }
}
// 更新 更新未入队的客户
bool bank::update() {
    //读取未入队的客户
    CallerMachine caller;
    for (int i = curClient; i < clientCount; i++) {
            //CallerMachine caller;
            QString name = ui.TbankClient->item(i, 1)->text();
            QString sexstr = ui.TbankClient->item(i, 2)->text();
            QString businessTypestr = ui.TbankClient->item(i, 3)->text();
            QString arriveDateTime = ui.TbankClient->item(i, 4)->text();
            QString computiontTime = ui.TbankClient->item(i, 5)->text();
            // 判断是否所有必填项都已填写
            if (name == ""||sexstr==""||arriveDateTime==""||computiontTime==""|| businessTypestr =="") {
                pop_up(QMessageBox::Warning, "警告", "必填项未填写\n请填写后重试");
                return false;
            }
            // 判断性别
            Sex sex; BussinessType bussinessType;
            if (sexstr == "男")   sex = Sex::MAN;
            else if(sexstr =="女")  sex = Sex::WOMAN;
            else {
                pop_up(QMessageBox::Warning, "警告", "性别不合法\n请核实后重试");
                return false;
            }
            // 判断业务类型
            if (businessTypestr == "一般业务")    bussinessType = BussinessType::COMMON;
            else    if(businessTypestr=="特殊业务")   bussinessType = BussinessType::SPECIAL;
            else {
                pop_up(QMessageBox::Warning, "警告", "业务类型不合法\n请核实后重试");
                return false;
            }
            // 判断到达时间 和 消耗时间
            QStringList  strs = arriveDateTime.split(":");
            if (strs.size()==2) {
                int arriveTime = 60 * (strs[0].toInt() - 8) + strs[1].toInt() - 30;
                if (arriveTime<0||arriveTime > 540) {
                    pop_up(QMessageBox::Warning, "警告", "到达时间不合法\n营业时间为8:30-17:30");
                    return false;
                }
                else if (computiontTime.toInt()>0) {
                    if (businessTypestr == "一般业务" && computiontTime.toInt() > 5) {
                        pop_up(QMessageBox::Warning, "警告", "消耗时间不合法\n一般业务的时间不超过5分钟");
                        return false;
                    }
                    else if (businessTypestr == "特殊业务" && computiontTime.toInt() > 15) {
                        pop_up(QMessageBox::Warning, "警告", "消耗时间不合法\n特殊业务的时间不超过15分钟");
                        return false;
                    }
                    else {
                        Client client = *new Client(caller, name.toStdString(), sex, bussinessType, arriveTime, computiontTime.toInt());
                        list.setElement(client, i);
                        qDebug() << list.getElement(i)->getNo();
                    }
                }else {
                    pop_up(QMessageBox::Warning, "警告", "消耗时间不合法\n请核实后重试");
                    return false;
                }
            }
            else {
                pop_up(QMessageBox::Warning, "警告", "到达时间不合法\n请按照14:20的样式输入");
                return false;
            }

    }
    //排序 编号
        //numbering();
    //重新显示
    //for (int i = curClient; i < clientCount; i++) {
    //    for (int j = 0; j < 6; j++) {
    //        //QTableWidgetItem* item = new QTableWidgetItem();
    //        QTableWidgetItem* item = ui.TbankClient->item(i,j);
    //        QString Text;
    //        switch (j) {
    //        case 0:Text = QString::fromStdString(clients[i].getNo()); break;
    //        case 1:Text = QString::fromStdString(clients[i].getName()); break;
    //        case 2:
    //            if (clients[i].getSex() == Sex::MAN)   Text = "男";
    //            else    Text = "女";
    //            break;
    //        case 3:
    //            if (clients[i].getBussinessType() == BussinessType::COMMON)   Text = "一般业务";
    //            else    Text = "特殊业务";
    //            break;
    //        case 4:Text = QString::fromStdString(clients[i].getReachDate().getTime()); break;
    //        case 5:Text = QString::number(clients[i].getComsumptionTime()); break;
    //        }
    //        item->setText(Text);
    //        //this->ui.TbankClient->setItem(i, j, item);
    //        //item->setTextAlignment(Qt::AlignCenter);
    //    }
    //}
    
    //排序 编号
    numbering();
    //重新显示
    for (int i = curClient; i < clientCount; i++) {
        for (int j = 0; j < 6; j++) {
            QString Text;
            switch (j) {
            case 0:Text = QString::fromStdString(list.getElement(i)->getNo());break;
            case 1:Text = QString::fromStdString(list.getElement(i)->getName());break;
            case 2:
                if (list.getElement(i)->getSex() == Sex::MAN)     Text = "男";
                else  Text = "女";
                break;
            case 3:
                if (list.getElement(i)->getBussinessType() == BussinessType::COMMON)   Text = "一般业务";
                else    Text = "特殊业务";
                break;
            case 4:Text = QString::fromStdString(list.getElement(i)->getReachDate().getTime());break;
            case 5:Text = QString::number(list.getElement(i)->getComsumptionTime()); break;
            }
            this->ui.TbankClient->item(i, j)->setText(Text);
        }
    }
    statistics();
    return true;
}
// 重置 实现清空数据
void bank::reset() {
    // 清除表格内容
    ui.TbankClient->clearContents();
    ui.TbankWidgets->clearContents();
    // 对窗口表格初始化（防止后续空指针报错）
    for (int i = 0; i < 10; i++) {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setTextAlignment(Qt::AlignCenter);
        ui.TbankWidgets->setItem(0, i, item);
    }
    // 清除统计
    ui.Tstatistics->clearContents();
    // 清空客户表格
    for (int row = ui.TbankClient->rowCount() - 1; row >= 0; row--)
    {
        ui.TbankClient->removeRow(row);
    }
    // 新建银行对象
    this->my_bank = *new Bank();
    this->clientCount = 0;
    this->curClient = 0;
    this->list.clear();
    // 重新恢复
    ui.add->setDisabled(false);
    ui.randomAdd->setDisabled(false);
    ui.update->setDisabled(false);
    ui.trading->setDisabled(false);
    ui.readFile->setDisabled(false);
    ui.readFile_2->setDisabled(false);
}
// 关门歇业 统计数据
void bank::close() {
    // 禁止添加客户操作
    ui.add->setDisabled(true);
    ui.randomAdd->setDisabled(true);
    ui.update->setDisabled(true);
    ui.trading->setDisabled(true);
    ui.readFile->setDisabled(true);
    ui.readFile_2->setDisabled(true);
    statistics();
    // 清理窗口的客户
    Widgets* widgets = &this->my_bank.getWidgets();
    for (int i = 1; i < 11; i++) {
        Queue<Client> que = widgets[i].widget.getQueue();
        while(!que.isempty()) {
            que.pop();
        }
        if(ui.TbankWidgets->item(0, widgets[i].num-1)!=nullptr)
        ui.TbankWidgets->item(0, widgets[i].num-1)->setText("");
    }
    // 删除未入队营业的客户
    for (int i = curClient; i < clientCount; i++) {
        ui.TbankClient->removeRow(curClient);
    }
    clientCount = curClient;
}
// 实现排序
void bank::sort(){
    /*
    0 编号
    1 姓名
    2 业务类型 
    3 窗口号
    */
    int index = ui.select->currentIndex();
    switch (index) {
    case 0:ui.TbankClient->sortByColumn(0, Qt::SortOrder::AscendingOrder); break;
    case 1:ui.TbankClient->sortByColumn(1, Qt::SortOrder::AscendingOrder); break;
    case 2:ui.TbankClient->sortByColumn(3, Qt::SortOrder::AscendingOrder); break;
    case 3:ui.TbankClient->sortByColumn(8, Qt::SortOrder::AscendingOrder); break;
    }
}
// 实现查找
void bank::check() {
    // 便于进行二分查找操作 优先进行排序操作
    sort();
    int low = 0;
    int high = ui.TbankClient->rowCount() - 1;
    int mid = 0;
    // 对所有行进行隐藏 特别地 当选中空白时 不做任何操作
    int index = ui.select->currentIndex();
    if (index != 4) {
        for (int i = 0; i < ui.TbankClient->rowCount(); i++)
        {
            ui.TbankClient->setRowHidden(i, true);
        }
    }
    /*
    0 编号:0
    1 姓名:1
    2 业务类型:3
    3 窗口号:8
    */
    QString str = ui.lineEdit->text();
    qDebug() << index;
    bool flag = false;
    switch (index) {
    case 0:
        while (low <= high) {
            mid = (low + high) / 2;
            if (str == ui.TbankClient->item(mid, 0)->text()) {
                ui.TbankClient->setRowHidden(mid, false);
                flag = true;
                break;
            }
            else if (str < ui.TbankClient->item(mid, 0)->text()) {
                high = mid - 1;
            }
            else {
                low = mid + 1;
            }
        }
        break;
    case 1:
        while (low <= high) {
            mid = (low + high) / 2;
            QString temp = ui.TbankClient->item(mid, 1)->text();
            if (str == temp) {
                ui.TbankClient->setRowHidden(mid, false);
                flag = true;
                for (int i = mid + 1;i < ui.TbankClient->rowCount();i++) {
                    if (str == ui.TbankClient->item(i, 1)->text()) {
                        ui.TbankClient->setRowHidden(i, false);

                    }
                    else {
                        break;
                    }
                }
                for (int i = mid - 1;i >= 0;i--) {
                    if (str == ui.TbankClient->item(i, 1)->text()) {
                        ui.TbankClient->setRowHidden(i, false);
                    }
                    else {
                        break;
                    }
                }
                break;
            }
            else if (str.localeAwareCompare(temp)<0) {
                high = mid - 1;
            }
            else {
                low = mid + 1;
            }
        }
        break;
    case 2:
        // 由于业务类型只有两种 此处采用直接遍历
        for (int i = 0;i < ui.TbankClient->rowCount();i++)
        {
            if (str == ui.TbankClient->item(i, 3)->text()) {
                ui.TbankClient->setRowHidden(i, false);
                flag = true;
            }
        }
        break;
    case 3:
        //if (ui.TbankClient->item(mid, 8) == NULL) {
        //    pop_up(QMessageBox::Icon::Warning, "警告", "未营业，无法查找！");
        //    flag = true;
        //    bank::reshow();
        //}
        //else
        {
            while (low <= high) {
                mid = (low + high) / 2;

                if (str == ui.TbankClient->item(mid, 8)->text()) {
                    ui.TbankClient->setRowHidden(mid, false);
                    flag = true;
                    for (int i = mid + 1; i < ui.TbankClient->rowCount(); i++) {
                        if (str == ui.TbankClient->item(i, 8)->text()) {
                            ui.TbankClient->setRowHidden(i, false);
                        }
                        else {
                            break;
                        }
                    }
                    for (int i = mid - 1; i >= 0; i--) {
                        if (str == ui.TbankClient->item(i, 8)->text()) {
                            ui.TbankClient->setRowHidden(i, false);
                        }
                        else {
                            break;
                        }
                    }
                    break;
                }
                else if (str < ui.TbankClient->item(mid, 8)->text()) {
                    high = mid - 1;
                }
                else {
                    low = mid + 1;
                }
            }
        }
        break;
    }
    if (index == 4) {
        pop_up(QMessageBox::Icon::Information, "提示", "请选择左侧下拉框后\n再进行后续操作");
        return;
    }
    if (!flag) {
        bank::reshow();
        bool ok = false;
        QInputDialog notFound;
        if (index == 3) {
            notFound.setInputMode(QInputDialog::IntInput);
            int widgetNum = notFound.getInt(this, "Not Found", "输入重新查找的值：",1,1,10,1,&ok);
            if (ok) {
                ui.lineEdit->setText(QString::number(widgetNum));
                check();
            }
        }
        else {
            notFound.setInputMode(QInputDialog::TextInput);
            QString findStr;
            findStr = notFound.getText(this, "Not Found", "输入重新查找的值：", QLineEdit::Normal, "", &ok);
            if (ok) {
                ui.lineEdit->setText(findStr);
                check();
            }
        }
        
    }
        
}
// 重新显示
void bank::reshow() {
    // 结束查找后 重新显示
    ui.lineEdit->clear();
    for (int i = 0; i < ui.TbankClient->rowCount(); i++)
    {
        ui.TbankClient->setRowHidden(i, false);
    }
    if (ui.select->currentIndex() == 4) {
        ui.TbankClient->sortByColumn(0, Qt::SortOrder::AscendingOrder);
    }
}
// 统计函数
void bank::statistics() {
    // 统计数据
    double commonClient = 0, specialClient = 0;
    double commonWaiting = 0, specialWaiting = 0;
    double commonTranact = 0, specialTranact = 0;
    int commonSojourn = 0, specialSojourn = 0;
    // 遍历客户 进行统计
    for (int i = 0; i < curClient; i++) {
        if (list.getElement(i)->getBussinessType() == BussinessType::COMMON) {
             commonWaiting += list.getElement(i)->getWaitingTime();
            commonTranact += list.getElement(i)->getComsumptionTime();
            commonSojourn += list.getElement(i)->getOverTime() - list.getElement(i)->getReachTime();
            commonClient++;
        }
        else {
            specialWaiting += list.getElement(i)->getWaitingTime();
            specialTranact += list.getElement(i)->getComsumptionTime();
            specialSojourn += list.getElement(i)->getOverTime() - list.getElement(i)->getReachTime();
            specialClient++;
        }
    }
    // 输出到页面
    for (int i = 0; i < 4; i++) {
        QTableWidgetItem* commonItem = new QTableWidgetItem();
        QTableWidgetItem* specialItem = new QTableWidgetItem();
        switch (i) {
        case 0:
            commonItem->setData(Qt::DisplayRole, commonWaiting / commonClient);
            specialItem->setData(Qt::DisplayRole, specialWaiting / specialClient);
            break;
        case 1:
            commonItem->setData(Qt::DisplayRole, commonTranact / commonClient);
            specialItem->setData(Qt::DisplayRole, specialTranact / specialClient);
            break;
        case 2:
            commonItem->setData(Qt::DisplayRole, commonSojourn / commonClient);
            specialItem->setData(Qt::DisplayRole, specialSojourn / specialClient);
            break;
        case 3:
            commonItem->setData(Qt::DisplayRole, commonClient);
            specialItem->setData(Qt::DisplayRole, specialClient);
            break;
        }
        commonItem->setTextAlignment(Qt::AlignCenter);
        specialItem->setTextAlignment(Qt::AlignCenter);
        this->ui.Tstatistics->setItem(0, i, commonItem);
        this->ui.Tstatistics->setItem(1, i, specialItem);
    }
}
